<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>EOS.IO: contracts/exchange/exchange.cpp File Reference</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="eos-logo.png"/></td>
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">EOS.IO
   &#160;<span id="projectnumber">DAWN3.0.1</span>
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="dir_ae9b15dd87e066e1908bbd90e8f38627.html">contracts</a></li><li class="navelem"><a class="el" href="dir_5dc13a1abd435123f7be5ea8dc751d1b.html">exchange</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="summary">
<a href="#namespaces">Namespaces</a> &#124;
<a href="#func-members">Functions</a>  </div>
  <div class="headertitle">
<div class="title">exchange.cpp File Reference</div>  </div>
</div><!--header-->
<div class="contents">

<p>defines an example exchange contract  
<a href="#details">More...</a></p>
<div class="textblock"><code>#include &lt;<a class="el" href="exchange_8hpp_source.html">exchange/exchange.hpp</a>&gt;</code><br />
<code>#include &lt;<a class="el" href="print_8hpp_source.html">eoslib/print.hpp</a>&gt;</code><br />
</div><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a name="namespaces"></a>
Namespaces</h2></td></tr>
<tr class="memitem:namespaceexchange"><td class="memItemLeft" align="right" valign="top"> &#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="namespaceexchange.html">exchange</a></td></tr>
<tr class="separator:"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a name="func-members"></a>
Functions</h2></td></tr>
<tr class="memitem:a4af1058806fad580b07a5da05729a6e0"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="namespaceexchange.html#a4af1058806fad580b07a5da05729a6e0">exchange::save</a> (const <a class="el" href="structaccount.html">account</a> &amp;a)</td></tr>
<tr class="separator:a4af1058806fad580b07a5da05729a6e0"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a13d8f4e165215611a7879766e215b855"><td class="memTemplParams" colspan="2">template&lt;typename Lambda &gt; </td></tr>
<tr class="memitem:a13d8f4e165215611a7879766e215b855"><td class="memTemplItemLeft" align="right" valign="top">void&#160;</td><td class="memTemplItemRight" valign="bottom"><a class="el" href="namespaceexchange.html#a13d8f4e165215611a7879766e215b855">exchange::modify_account</a> (<a class="el" href="group__types.html#ga3b44deb4b3b8d6ebab339d1263692117">account_name</a> a, Lambda &amp;&amp;modify)</td></tr>
<tr class="separator:a13d8f4e165215611a7879766e215b855"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a32b7d74a91010152d37cace85418e6f7"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="namespaceexchange.html#a32b7d74a91010152d37cace85418e6f7">exchange::apply_currency_transfer</a> (const currency::transfer &amp;transfer)</td></tr>
<tr class="separator:a32b7d74a91010152d37cace85418e6f7"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a7b4f0fec8b7a1d9c97dc3f5127872297"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="namespaceexchange.html#a7b4f0fec8b7a1d9c97dc3f5127872297">exchange::apply_eos_transfer</a> (const eosio::transfer &amp;transfer)</td></tr>
<tr class="separator:a7b4f0fec8b7a1d9c97dc3f5127872297"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a15c24418c4ed503becae07191757dee2"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="namespaceexchange.html#a15c24418c4ed503becae07191757dee2">exchange::match</a> (bid &amp;bid_to_match, <a class="el" href="structaccount.html">account</a> &amp;buyer, ask &amp;ask_to_match, <a class="el" href="structaccount.html">account</a> &amp;seller)</td></tr>
<tr class="separator:a15c24418c4ed503becae07191757dee2"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:abaf272c3c6eb0eb097e95916798b6b14"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="namespaceexchange.html#abaf272c3c6eb0eb097e95916798b6b14">exchange::apply_exchange_buy</a> (<a class="el" href="structexchange_1_1buy__order.html">buy_order</a> order)</td></tr>
<tr class="separator:abaf272c3c6eb0eb097e95916798b6b14"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a3832570cd9040ea2ceb28c4e43028a5e"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="namespaceexchange.html#a3832570cd9040ea2ceb28c4e43028a5e">exchange::apply_exchange_sell</a> (<a class="el" href="structexchange_1_1sell__order.html">sell_order</a> order)</td></tr>
<tr class="separator:a3832570cd9040ea2ceb28c4e43028a5e"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a539153eabc5f4d000b83633a50d78105"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="namespaceexchange.html#a539153eabc5f4d000b83633a50d78105">exchange::apply_exchange_cancel_buy</a> (<a class="el" href="structexchange_1_1order__id.html">order_id</a> order)</td></tr>
<tr class="separator:a539153eabc5f4d000b83633a50d78105"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ab6a1d78f1483a78bf31301a3a175d0ff"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="namespaceexchange.html#ab6a1d78f1483a78bf31301a3a175d0ff">exchange::apply_exchange_cancel_sell</a> (<a class="el" href="structexchange_1_1order__id.html">order_id</a> order)</td></tr>
<tr class="separator:ab6a1d78f1483a78bf31301a3a175d0ff"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a02fd73d861ef2e4aabb38c0c9ff82947"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="exchange_8cpp.html#a02fd73d861ef2e4aabb38c0c9ff82947">init</a> ()</td></tr>
<tr class="separator:a02fd73d861ef2e4aabb38c0c9ff82947"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a3c01edc4ed313916967237c16feb1446"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="exchange_8cpp.html#a3c01edc4ed313916967237c16feb1446">apply</a> (<a class="el" href="group__types.html#gaaa5d1cd013383c889537491c3cfd9aad">uint64_t</a> code, <a class="el" href="group__types.html#gaaa5d1cd013383c889537491c3cfd9aad">uint64_t</a> <a class="el" href="structeosio_1_1action.html">action</a>)</td></tr>
<tr class="separator:a3c01edc4ed313916967237c16feb1446"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table>
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
<div class="textblock"><dl class="section copyright"><dt>Copyright</dt><dd>defined in eos/LICENSE.txt This exchange contract assumes the existence of two currency contracts located at  and . These currency contracts have provided an API header defined in <a class="el" href="currency_8hpp.html">currency.hpp</a> which the exchange contract will use to process messages related to deposits and withdraws.</dd></dl>
<p>The exchange contract knows that the currency contracts require_notice() of both the sender and receiver; therefore, the exchange contract can implement a message handler that will be called anytime funds are deposited to or withdrawn from the exchange.</p>
<p>When tokens are sent to  from another account the exchange will credit the user's balance of the proper currency.</p>
<p>To withdraw from the exchange, the user simply reverses the "to" and "from" fields of the currency contract transfer message. The currency contract will require the "authority" of the exchange, but the exchange's <a class="el" href="exchange_8cpp.html#a02fd73d861ef2e4aabb38c0c9ff82947">init()</a> function configured this permission to allow <em>anyone</em> to transfer from the exchange.</p>
<p>To prevent people from stealing all the money from the exchange, the exchange's transfer handler requires both the authority of the receiver and asserts that the user has a sufficient balance on the exchange. Lacking both of these the exchange will kill the transfer.</p>
<p>The exchange and one of the currency contracts are forced to execute in the same thread anytime there is a deposit or withdraw. The transaction containing the transfer are already required to include the exchange in the scope by the currency contract.</p>
<p>creating, canceling, and filling orders do not require blocking either currency contract. Users can only deposit or withdraw to their own currency account. </p>
</div><h2 class="groupheader">Function Documentation</h2>
<a id="a3c01edc4ed313916967237c16feb1446"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a3c01edc4ed313916967237c16feb1446">&#9670;&nbsp;</a></span>apply()</h2>

<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">void apply </td>
          <td>(</td>
          <td class="paramtype"><a class="el" href="group__types.html#gaaa5d1cd013383c889537491c3cfd9aad">uint64_t</a>&#160;</td>
          <td class="paramname"><em>code</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype"><a class="el" href="group__types.html#gaaa5d1cd013383c889537491c3cfd9aad">uint64_t</a>&#160;</td>
          <td class="paramname"><em>action</em>&#160;</td>
        </tr>
        <tr>
          <td></td>
          <td>)</td>
          <td></td><td></td>
        </tr>
      </table>
</div><div class="memdoc">
<p>The apply method implements the dispatch of events to this contract </p>

</div>
</div>
<a id="a02fd73d861ef2e4aabb38c0c9ff82947"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a02fd73d861ef2e4aabb38c0c9ff82947">&#9670;&nbsp;</a></span>init()</h2>

<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">void init </td>
          <td>(</td>
          <td class="paramname"></td><td>)</td>
          <td></td>
        </tr>
      </table>
</div><div class="memdoc">

</div>
</div>
</div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.8.13
</small></address>
</body>
</html>
